home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / XPRB29 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  11.4 KB  |  405 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.DefaultDesktopManager;
  4. import com.sun.java.swing.DesktopManager;
  5. import com.sun.java.swing.Icon;
  6. import com.sun.java.swing.JComponent;
  7. import com.sun.java.swing.JInternalFrame;
  8. import com.sun.java.swing.LookAndFeel;
  9. import com.sun.java.swing.UIManager;
  10. import com.sun.java.swing.plaf.ComponentUI;
  11. import com.sun.java.swing.plaf.InternalFrameUI;
  12. import com.sun.java.swing.plaf.UIResource;
  13. import java.awt.Component;
  14. import java.awt.Container;
  15. import java.awt.Cursor;
  16. import java.awt.Dimension;
  17. import java.awt.IllegalComponentStateException;
  18. import java.awt.Insets;
  19. import java.awt.LayoutManager;
  20. import java.awt.event.MouseEvent;
  21. import java.awt.event.MouseListener;
  22. import java.awt.event.MouseMotionListener;
  23. import java.beans.PropertyChangeEvent;
  24. import java.beans.PropertyChangeListener;
  25. import java.io.Serializable;
  26. import java.util.EventListener;
  27. import java.util.EventObject;
  28.  
  29. public class BasicInternalFrameUI extends InternalFrameUI implements LayoutManager, PropertyChangeListener, Serializable, MouseListener {
  30.    protected JInternalFrame frame;
  31.    protected EventListener borderListener;
  32.    protected JComponent northPane;
  33.    protected JComponent southPane;
  34.    protected JComponent westPane;
  35.    protected JComponent eastPane;
  36.    protected static DesktopManager sharedDesktopManager;
  37.  
  38.    public BasicInternalFrameUI(JInternalFrame b) {
  39.    }
  40.  
  41.    protected void activateFrame(JInternalFrame f) {
  42.       this.getDesktopManager().activateFrame(f);
  43.    }
  44.  
  45.    public void addLayoutComponent(String name, Component c) {
  46.    }
  47.  
  48.    protected void closeFrame(JInternalFrame f) {
  49.       this.getDesktopManager().closeFrame(f);
  50.    }
  51.  
  52.    protected EventListener createBorderListener(JInternalFrame w) {
  53.       return new BorderListener(this);
  54.    }
  55.  
  56.    protected JComponent createEastPane(JInternalFrame w) {
  57.       return null;
  58.    }
  59.  
  60.    protected JComponent createNorthPane(JInternalFrame w) {
  61.       return new BasicInternalFrameTitlePane(w);
  62.    }
  63.  
  64.    protected JComponent createSouthPane(JInternalFrame w) {
  65.       return null;
  66.    }
  67.  
  68.    public static ComponentUI createUI(JComponent b) {
  69.       return new BasicInternalFrameUI((JInternalFrame)b);
  70.    }
  71.  
  72.    protected JComponent createWestPane(JInternalFrame w) {
  73.       return null;
  74.    }
  75.  
  76.    protected void deactivateFrame(JInternalFrame f) {
  77.       this.getDesktopManager().deactivateFrame(f);
  78.    }
  79.  
  80.    protected void deiconifyFrame(JInternalFrame f) {
  81.       this.getDesktopManager().deiconifyFrame(f);
  82.    }
  83.  
  84.    protected void deinstallMouseHandlers(JComponent c) {
  85.       if (c != null) {
  86.          if (this.borderListener instanceof MouseListener) {
  87.             ((Component)c).removeMouseListener((MouseListener)this.borderListener);
  88.          }
  89.  
  90.          if (this.borderListener instanceof MouseMotionListener) {
  91.             ((Component)c).removeMouseMotionListener((MouseMotionListener)this.borderListener);
  92.          }
  93.       }
  94.  
  95.    }
  96.  
  97.    protected DesktopManager getDesktopManager() {
  98.       if (this.frame.getDesktopPane() != null && this.frame.getDesktopPane().getDesktopManager() != null) {
  99.          return this.frame.getDesktopPane().getDesktopManager();
  100.       } else {
  101.          if (sharedDesktopManager == null) {
  102.             sharedDesktopManager = new DefaultDesktopManager();
  103.          }
  104.  
  105.          return sharedDesktopManager;
  106.       }
  107.    }
  108.  
  109.    public JComponent getEastPane() {
  110.       return this.eastPane;
  111.    }
  112.  
  113.    public Dimension getMaximumSize(JComponent x) {
  114.       return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  115.    }
  116.  
  117.    public Dimension getMinimumSize(JComponent x) {
  118.       return this.frame == x ? this.frame.getLayout().minimumLayoutSize(x) : new Dimension(0, 0);
  119.    }
  120.  
  121.    public JComponent getNorthPane() {
  122.       return this.northPane;
  123.    }
  124.  
  125.    public Dimension getPreferredSize(JComponent x) {
  126.       return this.frame == x ? this.frame.getLayout().preferredLayoutSize(x) : new Dimension(100, 100);
  127.    }
  128.  
  129.    public JComponent getSouthPane() {
  130.       return this.southPane;
  131.    }
  132.  
  133.    public JComponent getWestPane() {
  134.       return this.westPane;
  135.    }
  136.  
  137.    protected void iconifyFrame(JInternalFrame f) {
  138.       this.getDesktopManager().iconifyFrame(f);
  139.    }
  140.  
  141.    protected void installDefaults(JInternalFrame frame) {
  142.       Icon frameIcon = frame.getFrameIcon();
  143.       if (frameIcon == null || frameIcon instanceof UIResource) {
  144.          frame.setFrameIcon(UIManager.getIcon("InternalFrame.icon"));
  145.       }
  146.  
  147.       LookAndFeel.installBorder(frame, "InternalFrame.border");
  148.    }
  149.  
  150.    protected void installMouseHandlers(JComponent c) {
  151.       if (c != null) {
  152.          if (this.borderListener instanceof MouseListener) {
  153.             ((Component)c).addMouseListener((MouseListener)this.borderListener);
  154.          }
  155.  
  156.          if (this.borderListener instanceof MouseMotionListener) {
  157.             ((Component)c).addMouseMotionListener((MouseMotionListener)this.borderListener);
  158.          }
  159.       }
  160.  
  161.    }
  162.  
  163.    public void installUI(JComponent c) {
  164.       this.frame = (JInternalFrame)c;
  165.       this.frame.add(this.frame.getRootPane(), "Center");
  166.       this.installDefaults(this.frame);
  167.       this.borderListener = this.createBorderListener(this.frame);
  168.       this.frame.addPropertyChangeListener(this);
  169.       this.frame.setLayout(this);
  170.       this.setNorthPane(this.createNorthPane(this.frame));
  171.       this.setSouthPane(this.createSouthPane(this.frame));
  172.       this.setEastPane(this.createEastPane(this.frame));
  173.       this.setWestPane(this.createWestPane(this.frame));
  174.       this.installMouseHandlers(this.frame);
  175.       this.frame.setOpaque(true);
  176.       this.frame.setMinimumSize(new Dimension(120, 24));
  177.    }
  178.  
  179.    public void layoutContainer(Container c) {
  180.       Insets i = this.frame.getInsets();
  181.       int cx = i.left;
  182.       int cy = i.top;
  183.       int cw = this.frame.getWidth() - i.left - i.right;
  184.       int ch = this.frame.getHeight() - i.top - i.bottom;
  185.       if (this.getNorthPane() != null) {
  186.          Dimension size = this.getNorthPane().getPreferredSize();
  187.          this.getNorthPane().setBounds(cx, cy, cw, size.height);
  188.          cy += size.height;
  189.          ch -= size.height;
  190.       }
  191.  
  192.       if (this.getSouthPane() != null) {
  193.          Dimension size = this.getSouthPane().getPreferredSize();
  194.          this.getSouthPane().setBounds(cx, this.frame.getHeight() - i.bottom - size.height, cw, size.height);
  195.          ch -= size.height;
  196.       }
  197.  
  198.       if (this.getWestPane() != null) {
  199.          Dimension size = this.getWestPane().getPreferredSize();
  200.          this.getWestPane().setBounds(cx, cy, size.width, ch);
  201.          cw -= size.width;
  202.          cx += size.width;
  203.       }
  204.  
  205.       if (this.getEastPane() != null) {
  206.          Dimension size = this.getEastPane().getPreferredSize();
  207.          this.getEastPane().setBounds(cw - size.width, cy, size.width, ch);
  208.          cw -= size.width;
  209.       }
  210.  
  211.       if (this.frame.getRootPane() != null) {
  212.          this.frame.getRootPane().setBounds(cx, cy, cw, ch);
  213.       }
  214.  
  215.    }
  216.  
  217.    protected void maximizeFrame(JInternalFrame f) {
  218.       this.getDesktopManager().maximizeFrame(f);
  219.    }
  220.  
  221.    protected void minimizeFrame(JInternalFrame f) {
  222.       this.getDesktopManager().minimizeFrame(f);
  223.    }
  224.  
  225.    public Dimension minimumLayoutSize(Container c) {
  226.       Insets i = this.frame.getInsets();
  227.       Dimension result = this.frame.getRootPane().getMinimumSize();
  228.       result.width += i.left + i.right;
  229.       result.height += i.top + i.bottom;
  230.       if (this.getNorthPane() != null) {
  231.          Dimension d = this.getNorthPane().getMinimumSize();
  232.          result.width = Math.max(d.width, result.width);
  233.          result.height += d.height;
  234.       }
  235.  
  236.       if (this.getSouthPane() != null) {
  237.          Dimension d = this.getSouthPane().getMinimumSize();
  238.          result.width = Math.max(d.width, result.width);
  239.          result.height += d.height;
  240.       }
  241.  
  242.       if (this.getEastPane() != null) {
  243.          Dimension d = this.getEastPane().getMinimumSize();
  244.          result.width += d.width;
  245.          result.height = Math.max(d.height, result.height);
  246.       }
  247.  
  248.       if (this.getWestPane() != null) {
  249.          Dimension d = this.getWestPane().getMinimumSize();
  250.          result.width += d.width;
  251.          result.height = Math.max(d.height, result.height);
  252.       }
  253.  
  254.       return result;
  255.    }
  256.  
  257.    public void mouseClicked(MouseEvent e) {
  258.    }
  259.  
  260.    public void mouseEntered(MouseEvent e) {
  261.    }
  262.  
  263.    public void mouseExited(MouseEvent e) {
  264.    }
  265.  
  266.    public void mousePressed(MouseEvent e) {
  267.       ((BorderListener)this.borderListener).mousePressed(e);
  268.    }
  269.  
  270.    public void mouseReleased(MouseEvent e) {
  271.    }
  272.  
  273.    public Dimension preferredLayoutSize(Container c) {
  274.       Insets i = this.frame.getInsets();
  275.       Dimension result = this.frame.getRootPane().getPreferredSize();
  276.       result.width += i.left + i.right;
  277.       result.height += i.top + i.bottom;
  278.       if (this.getNorthPane() != null) {
  279.          Dimension d = this.getNorthPane().getPreferredSize();
  280.          result.width = Math.max(d.width, result.width);
  281.          result.height += d.height;
  282.       }
  283.  
  284.       if (this.getSouthPane() != null) {
  285.          Dimension d = this.getSouthPane().getPreferredSize();
  286.          result.width = Math.max(d.width, result.width);
  287.          result.height += d.height;
  288.       }
  289.  
  290.       if (this.getEastPane() != null) {
  291.          Dimension d = this.getEastPane().getPreferredSize();
  292.          result.width += d.width;
  293.          result.height = Math.max(d.height, result.height);
  294.       }
  295.  
  296.       if (this.getWestPane() != null) {
  297.          Dimension d = this.getWestPane().getPreferredSize();
  298.          result.width += d.width;
  299.          result.height = Math.max(d.height, result.height);
  300.       }
  301.  
  302.       return result;
  303.    }
  304.  
  305.    public void propertyChange(PropertyChangeEvent evt) {
  306.       String prop = evt.getPropertyName();
  307.       JInternalFrame f = (JInternalFrame)((EventObject)evt).getSource();
  308.       Object newValue = evt.getNewValue();
  309.       Object oldValue = evt.getOldValue();
  310.       if ("rootPane".equals(prop)) {
  311.          if (oldValue != null) {
  312.             this.frame.remove((Component)oldValue);
  313.          }
  314.  
  315.          if (newValue != null) {
  316.             this.frame.add((Component)newValue);
  317.          }
  318.       } else if ("isClosed".equals(prop)) {
  319.          if (newValue == Boolean.TRUE) {
  320.             this.closeFrame(f);
  321.          }
  322.       } else if ("isMaximum".equals(prop)) {
  323.          if (newValue == Boolean.TRUE) {
  324.             this.maximizeFrame(f);
  325.          } else {
  326.             this.minimizeFrame(f);
  327.          }
  328.       } else if ("isIcon".equals(prop)) {
  329.          if (newValue == Boolean.TRUE) {
  330.             this.iconifyFrame(f);
  331.          } else {
  332.             this.deiconifyFrame(f);
  333.          }
  334.       } else if ("isSelected".equals(prop)) {
  335.          if (newValue == Boolean.TRUE && oldValue == Boolean.FALSE) {
  336.             this.activateFrame(f);
  337.          } else if (newValue == Boolean.FALSE && oldValue == Boolean.TRUE) {
  338.             this.deactivateFrame(f);
  339.          }
  340.       }
  341.  
  342.    }
  343.  
  344.    public void removeLayoutComponent(Component c) {
  345.    }
  346.  
  347.    protected void replacePane(JComponent currentPane, JComponent newPane) {
  348.       if (currentPane != null) {
  349.          this.deinstallMouseHandlers(currentPane);
  350.          this.frame.remove(currentPane);
  351.       }
  352.  
  353.       if (newPane != null) {
  354.          this.frame.add(newPane);
  355.          this.installMouseHandlers(newPane);
  356.       }
  357.  
  358.    }
  359.  
  360.    public void setEastPane(JComponent c) {
  361.       this.eastPane = c;
  362.    }
  363.  
  364.    public void setNorthPane(JComponent c) {
  365.       this.replacePane(this.northPane, c);
  366.       this.northPane = c;
  367.    }
  368.  
  369.    public void setSouthPane(JComponent c) {
  370.       this.southPane = c;
  371.    }
  372.  
  373.    public void setWestPane(JComponent c) {
  374.       this.westPane = c;
  375.    }
  376.  
  377.    protected void uninstallDefaults(JInternalFrame frame) {
  378.       Icon frameIcon = frame.getFrameIcon();
  379.       if (frameIcon instanceof UIResource) {
  380.          frame.setFrameIcon((Icon)null);
  381.       }
  382.  
  383.       LookAndFeel.uninstallBorder(frame);
  384.    }
  385.  
  386.    public void uninstallUI(JComponent c) {
  387.       if (c != this.frame) {
  388.          throw new IllegalComponentStateException(this + " was asked to deinstall() " + c + " when it only knows about " + this.frame + ".");
  389.       } else {
  390.          this.uninstallDefaults(this.frame);
  391.          this.frame.setCursor(Cursor.getPredefinedCursor(0));
  392.          this.setNorthPane((JComponent)null);
  393.          this.setSouthPane((JComponent)null);
  394.          this.setEastPane((JComponent)null);
  395.          this.setWestPane((JComponent)null);
  396.          this.frame.setLayout((LayoutManager)null);
  397.          this.deinstallMouseHandlers(this.frame);
  398.          this.borderListener = null;
  399.          this.frame.removePropertyChangeListener(this);
  400.          this.frame.remove(this.frame.getRootPane());
  401.          this.frame = null;
  402.       }
  403.    }
  404. }
  405.